﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using SDT.CrawlSystem.Servers;
//using SDT.Libraries.DuplicateDetection;
using System.IO;
using System.Xml;
using System.Threading;
using SDT.Libraries.Utilities;
using SDT.DataService;
using MIL.Html;
using HtmlAgilityPack;

namespace Test
{
    public partial class Form1 : Form
    {
        //  private UCContainer container = UCContainer.Instance;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //ProcessFile();
            fixhtmltoxhtml(@"<a>sdfasfda <b>sfdsadfs</b>dfasf</div>");
        }

        public void fixhtmltoxhtml(string content)
        {

           HtmlAgilityPack.HtmlDocument htm = new HtmlAgilityPack.HtmlDocument();
           htm.LoadHtml(content);
            //MIL.Html.HtmlDocument mDocument = MIL.Html.HtmlDocument.Create(content);

           htm.OptionFixNestedTags = true;
           textBox1.Text = htm.DocumentNode.OuterHtml.ToString();
        
        }
        
        public void ProcessFile()
        {
            string sText = "";
            TextWriter w = null;
            //mở file đọc nội dung cũ
            using (StreamReader re = File.OpenText(@"\\172.16.1.76\Html\21\448\110405\677768.xml"))
            {
                sText = re.ReadToEnd();
            }
            //tạo cờ đánh dấu file đang được xử lý
            FileStream fs = null;
            try
            {
                while (fs == null)
                {
                    fs = File.Open(string.Format("{0}{1}.lck", @"\\172.16.1.76\Html\21\448\110405\", "677768"), FileMode.Create, FileAccess.Write, FileShare.None);
                    Thread.Sleep(20);
                }

                string sTemp = ReplaceAllOldImage(sText);

                if (!string.IsNullOrEmpty(sTemp))
                {
                    w = new FileInfo(@"\\172.16.1.76\Html\21\448\110405\677768.xml").CreateText();
                    w.Write(sTemp);
                }

            }
            catch { }
            finally
            {
                if (fs != null) fs.Close();
                if (w != null) w.Close(); w = null;
            }
        
        }

        public string ReplaceAllOldImage(string content)
        {
            lock (this)
            {
                StringReplacer strObj = new StringReplacer(content);
                IList<Spider_LinkDownload> l = new List<Spider_LinkDownload>();
                Spider_LinkDownload a = new Spider_LinkDownload();
                a.LinkURL = "http://congan.com.vn/img.testa";
                a.PathSaveFile = "http://kenhtin.net/image/anhdemo";

                Spider_LinkDownload b = new Spider_LinkDownload();
                b.LinkURL = "http://congan.com.vn/img.testb";
                b.PathSaveFile = "http://kenhtin.net/image/anhdemo";
                l.Add(a);
                l.Add(b);

                foreach (var itm in l)
                    strObj.AddStringReplacementPair(itm.LinkURL, itm.PathSaveFile);
                content = strObj.GetReplacedContent();

                return content;

            }

        }



        public int checktrycatch()
        {
            int i;
            i = 9;

            try
            {
                ///
                if (i > 2) return 1;

            }
            catch (Exception ex)
            {
                return 0;

            }
            finally
            {
                i = 10;
                ///dispose              
            }
            return 2;
        }

        public class Txt
        {
            public string StartWith = null;
            public int StartAt = 0;
            public string EndWith = null;

            public int MaxLength = 0;


        }

        //protected virtual string DoTxtFilter(string txt, string htm)
        //{
        //    if (this.DataSource == Regions.WEB) txt = htm;

        //    #region Check input data
        //    if (TheTxt == null || txt == null || txt.Length == 0)
        //        return "";
        //    #endregion

        //    #region Check start with & end with condition
        //    if (!string.IsNullOrEmpty(TheTxt.StartWith))
        //    {
        //        var s = txt.IndexOf(TheTxt.StartWith, TheTxt.StartAt);
        //        if (s >= TheTxt.StartAt)
        //        {
        //            if (!string.IsNullOrEmpty(TheTxt.EndWith))
        //            {
        //                var e = txt.IndexOf(TheTxt.EndWith, s + TheTxt.StartWith.Length);
        //                if (e > s)
        //                    return txt.Substring(s + TheTxt.StartWith.Length, e - s - TheTxt.StartWith.Length).Trim();
        //            }
        //            else
        //            {
        //                var ret = txt.Substring(s + TheTxt.StartWith.Length).Trim();
        //                if (TheTxt.MaxLength > 0)
        //                    ret = ret.Substring(0, Math.Min(TheTxt.MaxLength, ret.Length));
        //                return ret;
        //            }
        //        }
        //    }
        //    else if (!string.IsNullOrEmpty(TheTxt.EndWith))
        //    {
        //        var e = txt.IndexOf(TheTxt.EndWith, TheTxt.StartAt);
        //        if (e >= TheTxt.StartAt)
        //            return txt.Substring(TheTxt.StartAt, e);
        //    }
        //    #endregion

        //    return "";
        //}

        //20h07" 25/09/2010
        private DateTime GetDateVnMedia(string inputString)
        {
            try
            {
                inputString = inputString.Replace("h", ":");
                char cSp = '"';
                char cRep = ' ';
                inputString = inputString.Replace(cSp, cRep);
                inputString = RemoveWhitespaceWithSplit(inputString);
                inputString = inputString.Trim();
                //replace " and '
                inputString = inputString.Replace("'", "");

                string retFormatDate = "HH:mm dd/MM/yyyy";
                return DateTime.ParseExact(inputString, retFormatDate, null);
            }
            catch (Exception ex) { return DateTime.Now; }
        }

        public string RemoveWhitespaceWithSplit(string inputString)
        {
            StringBuilder sb = new StringBuilder();

            string[] parts = inputString.Split(new char[] { ' ', '\n', '\t', '\r', '\f', '\v' }, StringSplitOptions.RemoveEmptyEntries);

            int size = parts.Length;
            for (int i = 0; i < size; i++)
                sb.AppendFormat("{0} ", parts[i]);

            return sb.ToString();
        }


    }
}
